package com.weme.common.manager

import android.app.Application
import android.content.Context
import android.content.IntentFilter
import android.net.ConnectivityManager
import android.telephony.PhoneStateListener
import android.telephony.TelephonyManager
import android.util.Log
import androidx.lifecycle.*
import com.weme.common.BaseApplication
import com.weme.common.util.*

object SocketManager {
    private val TAG = this::class.java.simpleName

    /**
     * 生命周期是整个application的launch
     */
    private val applicationScope = ProcessLifecycleOwner.get().lifecycleScope

    /**
     * 网络连接的监听
     */
    private val connectivityReceiver = ConnectivityReceiver()

    /**
     * 手机信号状态监听
     */
    private val phoneStateChangeListener = PhoneStateChangeListener()

    /**
     * 应用的上下文
     */
    private var application: Application? = null

    private var telephonyManager: TelephonyManager? = null

      var seq = 0L

    /**
     * 不用做任何事情
     */
    fun init() = Unit

    init {
        applicationScope.launchWhenCreated {
            Log.e(TAG, "应用已经启动")
            application = BaseApplication.sInstance
            telephonyManager =
                application?.getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager
            registerConnectivityReceiver()
        }

        ProcessLifecycleOwner.get().lifecycle.addObserver(object : LifecycleEventObserver {
            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                if (event == Lifecycle.Event.ON_DESTROY) {
                    Log.e(TAG, "应用已经关闭")
                    unregisterConnectivityReceiver()
                }
            }

        })
    }

    /**
     * 注册一下网络状态的监听，用来重连应用
     */
    private fun registerConnectivityReceiver() {
        telephonyManager!!.listen(
            phoneStateChangeListener,
            PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
        )
        val filter = IntentFilter()
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
        application?.registerReceiver(connectivityReceiver, filter)
    }

    /**
     * 注销网络状态的监听
     */
    private fun unregisterConnectivityReceiver() {
        telephonyManager!!.listen(
            phoneStateChangeListener,
            PhoneStateListener.LISTEN_NONE
        )
        application?.unregisterReceiver(connectivityReceiver)
    }

    /**
     * 发送消息
     */
    fun send(msg: Any,cmdId: Int,seq:Long) {
        SocketConnectManager.send(msg,cmdId, seq)

    }


    //----------------------------

    /**
     * 收到的Network数据会从这个传递出去
     */
    val netWorkReceiver = SocketConnectManager.netWorkReceiver

    val netWorkReceiverForever = SocketConnectManager.netWorkReceiverForever
    /**
     * 返回自己在在意的数据类型
     *
     * @param cmdId 在意的cmdId
     *
     * @param care 变换
     *
     * @author 
     */
//    fun <T> careReceiver(cmdId: Int, care: (ByteString) -> T) =
//        SocketConnectManager.careReceiver(cmdId, care)


    fun close(){
        SocketConnectManager.disConnect()
    }

    fun connect(delay:Boolean = true){
        SocketConnectManager.startReConnect(delay)
    }

    fun isConnect():Boolean{
        return SocketConnectManager.isConnect()
    }
}